package com.zhehekeji.opsys.api.service.validate;

import java.util.ArrayList;
import java.util.List;

import org.apache.cxf.common.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.zhehekeji.opsys.api.code.LuckySignupResultCode;
import com.zhehekeji.opsys.api.common.util.SuperResp;
import com.zhehekeji.opsys.api.entity.TThemeMvpd;
import com.zhehekeji.opsys.api.entity.TThemeProduct;
import com.zhehekeji.opsys.api.service.validate.args.ThemeProductValidateArgs;

/**
 * @ClassName ThemeProductValidate
 * @Description TODO
 * @author chenweiliu chenwei.liu@163.com
 * @date 2016-3-16   下午3:41:10
 */
@Service
public class ThemeProductValidate extends ValidateService<ValidateRets<List<String>>, ThemeProductValidateArgs>{

	private final static Logger logger = LoggerFactory.getLogger(ThemeProductValidate.class);
	
	@Override
	public ValidateRets<List<String>> validate(ThemeProductValidateArgs validateArgs)
			throws Exception {
		ValidateRets<List<String>> validateRets = new ValidateRets<List<String>>();
		SuperResp resp = validateArgs.getResp();
		String movieCode = validateArgs.getMovieCode();
		String productID = validateArgs.getProductID();
		Long themeId = validateArgs.getThemeId();
		List<TThemeMvpd> tmpTThemeMvpdList = entityDao.getScrollData(TThemeMvpd.class, "o.ttheme.id=?1 and o.movieCode=?2",
				new Object[]{themeId, movieCode});
		List<String> tmpListProductID = null;
		if (tmpTThemeMvpdList != null && !tmpTThemeMvpdList.isEmpty()){
			tmpListProductID = new ArrayList<String>();
			for (TThemeMvpd tmpTThemeMvpd : tmpTThemeMvpdList){
				if (!StringUtils.isEmpty(tmpTThemeMvpd.getProductCode())){
					TThemeProduct tmpTThemeProduct = entityDao.get(TThemeProduct.class, "o.ttheme.id=?1 and o.productCode=?2 and o.status=1 ",
							new Object[]{themeId, tmpTThemeMvpd.getProductCode()});
				    if (tmpTThemeProduct != null){
				    	tmpListProductID.add(tmpTThemeMvpd.getProductCode());
				    }
				}
			}
		}
		if (tmpListProductID != null && !tmpListProductID.isEmpty()){
			if (StringUtils.isEmpty(productID)){
				logger.error("用户未订购专题产品，不满足参与条件！");
				resp.setResultCode(LuckySignupResultCode.PRODUCT_NOT_ORDER);
				resp.setResultDesc("用户未订购专题产品，不满足参与条件！");
				validateRets.setSuccess(false);
				return validateRets;
			}else{
				String[] tmpArrayProductID = productID.split(",");
				boolean isContains = false;
				for (String tmpStrProductID : tmpArrayProductID){
					if (tmpListProductID.contains(tmpStrProductID)){
						isContains = true;
						break;
					}
				}
				if (!isContains){
					logger.error("用户未订购专题产品，不满足参与条件！");
					resp.setResultCode(LuckySignupResultCode.PRODUCT_NOT_ORDER);
					resp.setResultDesc("用户未订购专题产品，不满足参与条件！");
					validateRets.setSuccess(false);
					return validateRets;
				}
			}
		}else{
			tmpListProductID = null;
			List<TThemeProduct> tmpTThemeProductList = entityDao.getScrollData(TThemeProduct.class, "o.ttheme.id=?1 and o.status=1", 
					new Object[]{themeId});
			if (tmpTThemeProductList != null && !tmpTThemeProductList.isEmpty()){
				tmpListProductID = new ArrayList<String>();
				for (TThemeProduct tmpTThemeProduct : tmpTThemeProductList){
					if (!StringUtils.isEmpty(tmpTThemeProduct.getProductCode())){
						tmpListProductID.add(tmpTThemeProduct.getProductCode());
					}
				}
			}
			if (tmpListProductID != null && !tmpListProductID.isEmpty()){
				if (StringUtils.isEmpty(productID)){
					logger.error("用户未订购专题产品，不满足参与条件！");
					resp.setResultCode(LuckySignupResultCode.PRODUCT_NOT_ORDER);
					resp.setResultDesc("用户未订购专题产品，不满足参与条件！");
					validateRets.setSuccess(false);
					return validateRets;
				}else{
					String[] tmpArrayProductID = productID.split(",");
					boolean isContains = false;
					for (String tmpStrProductID : tmpArrayProductID){
						if (tmpListProductID.contains(tmpStrProductID)){
							isContains = true;
							break;
						}
					}
					if (!isContains){
						logger.error("用户未订购专题产品，不满足参与条件！");
						resp.setResultCode(LuckySignupResultCode.PRODUCT_NOT_ORDER);
						resp.setResultDesc("用户未订购专题产品，不满足参与条件！");
						validateRets.setSuccess(false);
						return validateRets;
					}
				}
			}
		}
		validateRets.setEntity(tmpListProductID);
		return validateRets;
		
	}

}

